package com.locatme.modelBD;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.widget.Toast;

import com.main.locatme.JSONParser;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Julien on 21/03/14.
 */
public class DeleteEvent extends AsyncTask<String, String, String> {
    private FragmentActivity context;
    private static final String TAG_SUCCESS = "success";
    private ProgressDialog pDialog;
    JSONParser jsonParser = new JSONParser();
    private static final String url_connect = "http://locatme.alwaysdata.net/locateme/delete_event.php";

    public void setContext(FragmentActivity context) {
        this.context = context;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(context);
        pDialog.setMessage("Connexion au serveur ...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    @Override
    protected String doInBackground(String... strings) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("id", strings[0]));
        JSONObject json = jsonParser.makeHttpRequest(url_connect,
                "POST", params);


        try {

            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {

                context.runOnUiThread(new Runnable() {
                    public void run() {

                        Toast.makeText(context, " Enregistrement effectué ", Toast.LENGTH_LONG).show();
                    }
                });

                context.finish();
            } else {
                context.runOnUiThread(new Runnable() {
                    public void run() {

                        Toast.makeText(context, " Enregistrement échoué ", Toast.LENGTH_LONG).show();
                    }
                });
                context.finish();

            }
        } catch (JSONException e) {

            Log.d("Exeception de json : ", e.toString());
        }
        pDialog.dismiss();
        return null;
    }

}
